package com.epoint.mobileoa.personalized.epointcompany.ui.kaoqing;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.personalized.epointcompany.config.ConfigKey;
import com.epoint.mobileoa.personalized.epointcompany.model.QiandaoInfoModel;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2012-8-6 下午4:22:57
 * @annotation 
 */
public class KaoQingDetailView extends SuperView {

	private TextView tvName,tvDept,tvTime,tvAddress,tvRecord_tag;
	private Button btQd;
	private ListView lvRecordInfo;
	private int taskid;//1获取当前位置,2提交数据
	private String address;
	private List<QiandaoInfoModel> qdList = new ArrayList<QiandaoInfoModel>();
	private QianDaoInfoAdapter adapter;
	private SubLocationListener subLocationListener;
	public static LocationManager lm;
	public static Location klocation;

	@Override
	public void initControls() {

		lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

		subLocationListener = new SubLocationListener();

		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, subLocationListener);

		showTopRightBtn("刷新");

		lvRecordInfo = (ListView) findViewById(R.id.lvRecordInfo);
		tvName = (TextView) findViewById(R.id.tvName);
		tvDept = (TextView) findViewById(R.id.tvDept);
		tvTime = (TextView) findViewById(R.id.tvTime);
		tvAddress = (TextView) findViewById(R.id.tvAddress);
		tvRecord_tag = (TextView) findViewById(R.id.tvRecord_tag);
		btQd = (Button) findViewById(R.id.btQd);
		btQd.setOnClickListener(this);
		setTextViewFontColor(tvRecord_tag);

		String QDInfo = DBHelp.getConfigValue(this, ConfigKey.QDInfo+fp.getUserguid());
		if(!QDInfo.equals("")){
			qdList = QDTask.XmlAnalysis("<root>"+QDInfo+"</root>");
		}
		adapter = new QianDaoInfoAdapter();
		lvRecordInfo.setAdapter(adapter);

		getLocation();
	}

	private void getLocation(){
		taskid = 1;
		showDialog_wait("正在获取当前位置...");
		Task task = new KaoQingDetailViewTask(this,null);
		MainService.addTask(task);
	}

	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			getLocation();
		}
		if(v==btQd){

			if(StringHelp.isEqualBlank(address)){
				ToastHelp.showToast(this, "请先获取当前位置");
				return;
			}

			taskid = 2;
			showDialog_wait("正在提交信息...");
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put("address", address);
			Task task = new QDTask(this, params);
			MainService.addTask(task);
		}
	}

	@Override
	public void refresh(Object... param) {
		m_Dialog.dismiss();
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==1){
				String bs = param[1].toString();
				if(bs.equals(KaoQingDetailViewTask.Get_Address_Failure)){
					ToastHelp.showToast(this, "获取位置失败，请重试");
				}
				else{
					String displayname = fp.getDisplayname();
					String deptname = DBHelp.getUserOuname(this,fp.getUserguid());
					tvName.setText(Html.fromHtml(StringHelp.getHtmlFormat1(this, "姓名：", displayname)));
					tvDept.setText(Html.fromHtml(StringHelp.getHtmlFormat1(this, "部门：", deptname)));
					tvTime.setText(Html.fromHtml(StringHelp.getHtmlFormat1(this, "时间：", DateHelp.convertDate(new Date(), DateHelp.DateFormat_24))));
					address= bs;
					tvAddress.setText(Html.fromHtml(StringHelp.getHtmlFormat1(this, "地点：", address)));
				}
			}
			else if(taskid==2){
				String bs = param[1].toString();
				if(bs.contains("<Record><AttendTime>")){
					String formatXml = "<root>"+bs+"</root>";
					DBHelp.setConfigValue(this, ConfigKey.QDInfo+fp.getUserguid(), bs);
					qdList = QDTask.XmlAnalysis(formatXml);
					adapter.notifyDataSetChanged();
				}
				else{
					ToastHelp.showToast(this, "签到失败，请重试");
				}
			}
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.kaoqingdetailview_pure;
	}

	@Override
	public String setTitle() {
		return "考勤签到";
	}



	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(lm!=null){
			lm.removeUpdates(subLocationListener);
			lm = null;
		}
	}

	class QianDaoInfoAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return qdList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SuperView con = KaoQingDetailView.this;
			TextView recard = new TextView(con);
			QiandaoInfoModel q = qdList.get(position);
			recard.setText(Html.fromHtml("<font color="+con.getBlackOrWhiteColor()+"> 时间：" + q.AttendTime + "</font> <br/> <font color ="+con.getYGColor()+">地点：" + q.AttendLocation+"</font"));
			recard.setTextColor(Color.rgb(255, 201, 14));
			recard.setTextSize(14);
			return recard;
		}

	}

	class SubLocationListener implements LocationListener{

		@Override
		public void onLocationChanged(Location location) {
			//实时更新位置
			klocation = location;
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onProviderDisabled(String provider) {

		}

	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}
}
